import operate from '@dfeidao/atom-nodejs/db/operate';

import query from '@dfeidao/atom-nodejs/db/query';


// import { systemid } from './config';
interface Message {
	group_id: string;
	role_allot_id: string;
	productid: string;
}
interface Data {
	group_id: string;
}
export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';

	const { del, exec } = operate(action_id, session_id, systemid);

	const { prepare: p, exec: e } = query(action_id, session_id, systemid);

	p('allot_conditions_group', ['group_id'], { productid: msg.productid, role_allot_id: msg.role_allot_id, parent_group_id: msg.group_id }, 199, 1, [], []);
	const [data] = await e<Data>();
	// console.log(data)
	data.forEach((v) => {
		del('allot_conditions', { productid: msg.productid, group_id: v.group_id });
	});


	del('allot_conditions', { productid: msg.productid, group_id: msg.group_id });

	del('allot_conditions_group', { productid: msg.productid, role_allot_id: msg.role_allot_id, group_id: msg.group_id });

	del('allot_conditions_group', { productid: msg.productid, role_allot_id: msg.role_allot_id, parent_group_id: msg.group_id });
	const ret = await exec();
	if (ret.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
